package org.zsk.cs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.zsk.cs.domain.po.User;
import org.zsk.cs.domain.vo.UserVo;
import org.zsk.cs.response.ResultBody;
import org.zsk.cs.service.UserService;
import org.zsk.cs.util.UserUtils;
import org.zsk.fastdev.assign.Assign;

import java.util.List;

/**
 * <p>
 * 用户 controller
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/29
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口", description = "提供用户相关的 Rest API")
public class UserController {

    private final UserService userService;
    private final Assign assign;
    private final PasswordEncoder passwordEncoder;

    @Autowired
    public UserController(UserService userService, Assign assign, PasswordEncoder passwordEncoder) {
        this.userService = userService;
        this.assign = assign;
        this.passwordEncoder = passwordEncoder;
    }

    @ApiOperation("新增用户接口")
    @PostMapping("/a")
    public ResultBody add(@RequestBody UserVo userVo) throws Exception {
        User user=new User();
        assign.assign(userVo,user);
        return ResultBody.success(userService.save(user));
    }

    @PutMapping("/u")
    public ResultBody update(@RequestBody UserVo userVo) throws Exception {
        User user=new User();
        assign.assign(userVo,user);
        return ResultBody.success(userService.updateById(user));
    }

    @DeleteMapping("/d/{ids}")
    public ResultBody delete(@PathVariable List<Integer> ids) throws Exception {
        return ResultBody.success(userService.removeByIds(ids));
    }

    @GetMapping("/g")
    public ResultBody page(UserVo userVo) throws Exception {
        Page<User> page=new Page<>();
        assign.assign(userVo,page,"$current,$size");
        return ResultBody.success(userService.page(page));
    }

    @PutMapping("/changepass")
    public ResultBody userChangePassword(@RequestBody UserVo userVo) throws Exception {
        User user=new User();
        if (!passwordEncoder.matches(userVo.getOldPassword()
                ,UserUtils.getCurrentUser().getPassword())) {
            throw new IllegalArgumentException("旧密码错误");
        }
        userVo.setId(UserUtils.getCurrentUser().getId());
        assign.assign(userVo,user,"$id,$password");
        return ResultBody.success(userService.userChangePassword(user));
    }

    @PutMapping("/resetpass")
    public ResultBody userResetPassword(@RequestBody UserVo userVo) throws Exception {
        User user=new User();
        assign.assign(userVo,user,"$password,$email");
        return ResultBody.success(userService.userResetPassword(user,userVo.getEmailVerifyCode()));
    }

    @PutMapping("/changeinfo")
    public ResultBody userChangePersonalInfo(@RequestBody UserVo userVo) throws Exception {
        User user=new User();
        userVo.setId(UserUtils.getCurrentUser().getId());
        assign.assign(userVo,user,"$id,hobby,mobile,job,age,email");
        return ResultBody.success(userService.userChangePersonalInfo(user));
    }

    @PutMapping("/disable")
    public ResultBody disableUser(@RequestBody List<Integer> ids) throws Exception {
        return ResultBody.success(userService.disableUser(ids));
    }

    @GetMapping("/search")
    public ResultBody searchUser(UserVo userVo) throws Exception {
        User user=new User();
        Page<User> page=new Page<>();
        assign.assign(userVo,user,"username,enable,locked");
        assign.assign(userVo,page,"$current,$size");
        return ResultBody.success(userService.searchUser(user,page));
    }

    @GetMapping("/gall")
    public ResultBody getAll() {
        return ResultBody.success(userService.list());
    }

    @PostMapping("/register")
    public ResultBody register(@RequestBody UserVo userVo) throws Exception {
        User user=new User();
        assign.assign(userVo,user,"$username,$password");
        return ResultBody.success(userService.save(user));
    }
}
